__author__ = '天祥'

from bs4 import BeautifulSoup

#获取表格单元格数据
def getMsg():
    fin = open('credit.html', 'r')
    page_data = fin.read()
    soup = BeautifulSoup(page_data, 'html.parser')
    tds = soup.find_all('td')
    return tds

#提取单元格数据
def change(tds):
    message = []
    for temptd in tds:
        tempSoup = BeautifulSoup(str(temptd), 'html.parser')
        message.append(tempSoup.td.string)
    return message
#格式化单元格数据
def getGrade(message):
    gradeMsg = []
    for i in range(0, len(message), 11):
        data = message[i: i+11]
        temp = {
            'class_num': data[0],
            'class_name': data[1],
            'class_type': data[2],
            'credit': data[3],
            'teacher': data[4],
            'institute': data[5],
            'learning_type': data[6],
            'year': data[7],
            'term': data[8],
            'grade': data[9],
        }
        gradeMsg.append(temp)
    return gradeMsg

if __name__ == '__main__':
    print(getGrade(change(getMsg())))
